
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maHD = ISS.SESSION.getSession('session_maHD');

    var bill = {};
    bill.maHD = _maHD;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editBill',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(bill),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllMemberInBill',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMemberToView(responseContent);
        }
    });

}

function loadDataComplete(bill){
    ISS.DOM.setVal('#BillCommon_Code', bill.maHD);
    ISS.DOM.setVal('#BillCommon_Receiver', bill.hoTenNN);
    ISS.DOM.setVal('#BillCommon_ReceiverPhone', bill.soDTNN);
    ISS.DOM.setVal('#BillCommon_ReceiverAddress', bill.diaChiNN);
    ISS.DOM.setVal('#BillCommon_DateCreate', ISS.DATE.getFullDate(new Date(bill.ngayLapHD)));
    ISS.DOM.setVal('#BillCommon_DateBegin', ISS.DATE.getFullDate(new Date(bill.ngayDat)));
    ISS.DOM.setVal('#BillCommon_DateEnd', ISS.DATE.getFullDate(new Date(bill.ngayGiao)));
    ISS.DOM.setVal('#BillCommon_Status_Hidden', bill.trangThai);
    ISS.DOM.setVal('#BillCommon_Finish_Hidden', bill.hoanTat);
    ISS.DOM.setVal('#BillCommon_Note', bill.ghiChu);
    ISS.DOM.setVal('#BillCommon_Total', formatCurency(bill.tongTien));
    ISS.DOM.setVal('#BillCommon_Member_Hidden', bill.thongTinThanhVien.maTV);
    ISS.DOM.setVal('#BillCommon_Member_LastName', bill.thongTinThanhVien.ho);
    ISS.DOM.setVal('#BillCommon_Member_FirstName', bill.thongTinThanhVien.ten);
    ISS.DOM.setVal('#BillCommon_Member_Address', bill.thongTinThanhVien.diaChi);
    ISS.DOM.setVal('#BillCommon_Member_Phone', bill.thongTinThanhVien.soDT);

    var finish = ISS.DOM.getVal('#BillCommon_Finish_Hidden');
    var html = '';
        if (finish == 1){
            html += '<option value="1" selected="selected" >Hoàn tất</option>';
            html += '<option value="0" >Chưa hoàn tất</option>';
        }
        else{
            html += '<option value="1" >Hoàn tất</option>';
            html += '<option value="0" selected="selected" >Chưa hoàn tất</option>';
        }

    ISS.DOM.html('#BillCommon_Finish', html);

    var status = ISS.DOM.getVal('#BillCommon_Status_Hidden');
    var htmlstatus = '';
    if (status == 0){
        htmlstatus += '<option value="0" selected="selected" >Chưa giao hàng</option>';
        htmlstatus += '<option value="1" >Đã giao hàng</option>';
    }
    else if (status == 1){
        htmlstatus += '<option value="0">Chưa giao hàng</option>';
        htmlstatus += '<option value="1" selected="selected" >Đã giao hàng</option>';
    }

    ISS.DOM.html('#BillCommon_Status', htmlstatus);

}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#BillCommon_Edit1', 'click', function () {
        sendInfoEdit();
    });

    ISS.EVENT.bindEvent('#BillCommon_Member', 'change', function () {
        var _maTV = ISS.DOM.getVal('#'+this.id);
        var member = {};
        member.maTV = _maTV
        ISS.DOM.setVal('#BillCommon_Member_Hidden', _maTV);

        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/bill_toGetMember',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(member),
            success: function (data) {
                var responseContent = data.responseObject;
                loadReceiverDataComplete(responseContent);
            }
        });

    });
}

function loadReceiverDataComplete(receiver){
    ISS.DOM.setVal('#BillCommon_Member_Hidden', receiver.maTV);
    ISS.DOM.setVal('#BillCommon_Member_LastName', receiver.ho);
    ISS.DOM.setVal('#BillCommon_Member_FirstName', receiver.ten);
    ISS.DOM.setVal('#BillCommon_Member_Address', receiver.diaChi);
    ISS.DOM.setVal('#BillCommon_Member_Phone', receiver.soDT);

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllMemberInBill',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMemberToView(responseContent);
        }
    });
}

function sendInfoEdit() {
    var bill = {};
    var member = {};

    bill.maHD = ISS.DOM.getVal('#BillCommon_Code');
    bill.hoTenNN = ISS.DOM.getVal('#BillCommon_Receiver');
    bill.soDTNN = ISS.DOM.getVal('#BillCommon_ReceiverPhone');
    bill.diaChiNN = ISS.DOM.getVal('#BillCommon_ReceiverAddress');
    bill.ngayLapHD = ISS.DOM.getVal('#BillCommon_DateCreate');
    bill.ngayDat = ISS.DOM.getVal('#BillCommon_DateBegin');
    bill.ngayGiao = ISS.DOM.getVal('#BillCommon_DateEnd');
    bill.hoanTat = ISS.DOM.getVal('#BillCommon_Finish');
    bill.ghiChu = ISS.DOM.getVal('#BillCommon_Note');
    bill.tongTien = ISS.DOM.getVal('#BillCommon_Total');
    member.maTV  = ISS.DOM.getVal('#BillCommon_Member');
    bill.thongTinThanhVien = member;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerBillEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(bill),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(color) {
    if(color) {
        ISS.WINDOW.location('/jsp/admin/ad-bill-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function mapAllMemberToView(member) {
    var billMember = ISS.DOM.getVal('#BillCommon_Member_Hidden');
    var n = '';
    var html = '';
    for(var i=0; i<member.length; i++) {
        if (member[i].maTV == billMember){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + member[i].maTV + '" ' + n + '  >' + member[i].maTV + '</option>';
    }
    ISS.DOM.html('#BillCommon_Member', html);
}
